﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_FillReturnInfo : UCBase 
{
    public UserControls_FillReturnInfo()
    {
        Name = "退货申请";
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (_Customer == null)
        {
            Response.Redirect("Default.aspx?PN=Login");
        }
        else
        {
            LoadSaleNo();
            this.SalesNo.Visible = false;
            gvOrders.DataSource = (DataTable)Session["datatable"];
            gvOrders.DataBind();
        }
    }

    private void LoadSaleNo()
    { 
        long ShopID = _Site.ID;
        wcSaleNo.DataSource = new DAL.Tables.tbl_Shop_SalesMaster().Open("ID,SalesNO", "ShopID=" + ShopID + " and CustomerID=" + _Customer.ID + " and Status=4", "");
        wcSaleNo.DataTextField = "SalesNO";
        wcSaleNo.DataValueField = "SalesNO";
        wcSaleNo.DataBind();
    }
    /// <summary>
    /// 点击查询按钮，查询出此订单的相关商品信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["SaleNo"] = wcSaleNo.DisplayValue;
        this.SalesNo.Visible = true;
        long ShopID = _Site.ID;
        DataTable dt = null;

        string Condtion = "";

        if (txtGoodNo.Text.Equals(""))
        {
            //dt = BLL.Sales__Detail.GetSalesDetail("CustomerID=" + ((Entity.Customer)_Customer).Id + " and ShopID=" + shopID + " and SalesNO='" + wcSaleNo.DisplayValue  + "'");
            Condtion = "CustomerID=" + _Customer.ID + " and ShopID=" + ShopID + " and SalesNO='" + wcSaleNo.DisplayValue + "'";
            dt = new DAL.Tables.tbl_Shop_SalesDetail().Open("", Shove._Web.Utility.FilteSqlInfusion(Condtion), "");
        }
        else
        {
            //dt = BLL.Sales__Detail.GetSalesDetail("CustomerID=" + ((Entity.Customer)_Customer).Id + " and ShopID=" + shopID + " and SalesNO='" + wcSaleNo.DisplayValue  + "' and GoodNO='" + txtGoodNo.Text + "'");
            Condtion = "CustomerID=" + _Customer.ID + " and ShopID=" + ShopID + " and SalesNO='" + wcSaleNo.DisplayValue + "' and GoodNO='" + txtGoodNo.Text + "'";
            dt = new DAL.Tables.tbl_Shop_SalesDetail().Open("",Shove._Web.Utility.FilteSqlInfusion(Condtion) , "");
        }

        if(dt != null && dt.Rows.Count > 0)
        {
            gvOrders.DataSource = dt;
            gvOrders.DataBind();
            Session["datatable"] = dt;
        }
        else
        {
            gvOrders.Visible = false;
            this.SaleDetail.Attributes.Add("class", "red");
            if (txtGoodNo.Text.Equals(""))
            {
                this.SaleDetail.InnerText = "无此订单号请重新输入！";
            }
            else
            {
                this.SaleDetail.InnerText = "此订单号无此商品编号请重新输入！";
                
            }
        }
    }
    protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //设置表格样式
        e.Row.Attributes.Add("class", "black12");
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Style.Add("background-image", "url(images/bg_hui_23_2.jpg)");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.BackColor = System.Drawing.Color.FromName("#FFFFFF");

            //为checkbox添加点击事件
            CheckBox check = (CheckBox)e.Row.FindControl("cbCheck");

            check.Attributes.Add("onclick","clicks('"+gvOrders.DataKeys[e.Row.RowIndex].Values[0].ToString()+"','"+e.Row.RowIndex+"',this)");
        }
    }

    /// <summary>
    /// 申请退货
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        long ShopID = _Site.ID;

        string[] goodNo = txtGoodNo.Text.Split(',');
        for (int i = 0; i < goodNo.Length; i++)
        {
            //DataTable dt = BLL.Sales__Detail.GetSalesDetail("CustomerID=" + ((Entity.Customer)_Customer).Id + " and ShopID=" + shopID + " and SalesNO='" + Session["SaleNo"].ToString() + "' and GoodNO='" + goodNo[i].ToString() + "'");
            DataTable dt = new DAL.Tables.tbl_Shop_SalesDetail().Open("", "CustomerID=" + _Customer.ID + " and ShopID=" + ShopID + " and SalesNO='" + Session["SaleNo"].ToString() + "' and GoodNO='" + goodNo[i].ToString() + "'", "");
           
            if (dt == null || dt.Rows.Count < 1)
            {
                Shove._Web.JavaScript.Alert(this.Page, "你输入的商品编号有误！");

                return;
            }
        }

        //插入数据到退货单主表
        //Entity.BackGood b = new Entity.BackGood();
        //b.BackGoodNO = BLL.CommBLL.GetOrderNO("BackGoodNO", "tbl_BackGood_Master");//获取自动生成的退货单号
        //b.CustomerID = ((Entity.Customer)_Customer).Id;
        //b.TargetShopID = shopID;
        //b.Status = 0;
        //b.CreateOperID = ((Entity.Customer)_Customer).Id;
        //b.CreateDate = DateTime.Now;
        //b.Type = Shove._Convert.StrToInt( rblType.SelectedValue,0);
        //b.ReturnSeason = txtReturnReason.Text;
        //b.DeclineSeason = "";
        //int id = BLL.BackGoodBLL.AddBackGoodMaster(b);

        ////插入数据到退货单明细表
        //for (int i = 0; i < gvOrders.Rows.Count; i++)
        //{
        //    CheckBox check = (CheckBox)gvOrders.Rows[i].FindControl("cbCheck");
        //    if (check.Checked == true)
        //    {
        //        Entity.BackGoodDetail detail = new Entity.BackGoodDetail();
        //        detail.BackGoodID = id;
        //        detail.GoodID = Shove._Convert.StrToLong( gvOrders.DataKeys[i].Values[1].ToString(),0);
        //        detail.Amount = Shove._Convert.StrToInt(gvOrders.Rows[i].Cells[4].Text, 0);
        //        detail.GoodColor = gvOrders.Rows[i].Cells[3].Text;
        //        detail.GoodNorm = gvOrders.Rows[i].Cells[2].Text;
        //        detail.Price = Shove._Convert.StrToDouble( gvOrders.DataKeys[i].Values[2].ToString(),0);
               
        //        BLL.BackGoodDetailBLL.InsertBackGoodDetail(detail);
        //    }
        //}

        Response.Redirect("Default.aspx?PN=member_home&Type=ReturnGood.ascx");
    }
}
